<?php

namespace App\Models;

use App\Common\Constant\Params;
use App\Services\IStorageService;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Debugbar;

class OrderStatus extends BaseModel
{

    protected $table = 'orders_status';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'upd_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'order_id',
        'merchant_status',
        'master_status',
        'logistics_status',
        'except_status',
        'refund_status',
        'arbitrate_status',
        'evaluate_status',
        'appoint_time',
        'door_time',
        'deliver_time',
        'deliver_imgs',
        'deliver_except',
        'finish_time',
        'finish_imgs',
        'finish_ticket_img',
        'tmall_check_img',
        'finish_message',
        'order_success_time'
    ];

    protected $casts = [
        'appoint_time' => 'datetime',
        'door_time' => 'datetime',
        'deliver_time' => 'datetime',
        'finish_time' => 'datetime',
        'order_success_time' => 'datetime',
        'deliver_imgs' => 'array',
        'finish_imgs' => 'array',
    ];

    protected $touches = ['order'];

    public function order(){
        return $this->belongsTo(Order::class,'order_id');
    }

    /**
     * 是否已到货文本
     */
    public function getLogisticsStatusTextAttribute()
    {
        return Params::$LOGISTICS_STATUS[$this->logistics_status];
    }

    /**
     * 订单交易已取消
     * canceled
     */
    public function getCanceledAttribute()
    {
        return $this->except_status == Params::ORDER_EXCEPT_CANCELED;
    }

    /**
     * 订单提交过退款申请
     * have_refund
     */
    public function getHaveRefundAttribute()
    {
        return $this->refund_status != Params::ORDER_REFUND_NORMAL;
    }

    /**
     * 订单提交过仲裁申请
     * have_arbitrate
     */
    public function getHaveArbitrateAttribute()
    {
        return $this->arbitrate_status != Params::ORDER_ARBITRATE_NORMAL;
    }

    /**
     * 订单交易已关闭（已退款）
     * closed
     */
    public function getClosedAttribute()
    {
        return in_array($this->except_status,[Params::ORDER_EXCEPT_REFUNDED,Params::ORDER_EXCEPT_ARBITRATED])
            && ($refund = OrderRefund::where('order_id',$this->order_id)->first())
            && ($refund->refund_type == Params::ORDER_REFUND_FULL)
            && (!$refund->arbitrate_result_type || ($refund->arbitrate_result_type == Params::ORDER_ARBITRATE_RESULT_AGREE_FULL));
    }

    /**
     * 订单已退款完成
     * refunded
     */
    public function getRefundedAttribute()
    {
        return $this->except_status == Params::ORDER_EXCEPT_REFUNDED;
    }

    /**
     * 订单已仲裁退款完成
     * arbitrated
     */
    public function getArbitratedAttribute()
    {
        return $this->except_status == Params::ORDER_EXCEPT_ARBITRATED;
    }

    /**
     * 已雇佣
     */
    public function getEmployedAttribute()
    {
        return ($this->master_status == Params::ORDER_MASTER_STATUS_WAITING)
            || ($this->master_status == Params::ORDER_MASTER_STATUS_SERVING);
    }

    /**
     * 已预约客户上门时间
     */
    public function getAppointedAttribute()
    {
        return $this->master_status == Params::ORDER_MASTER_STATUS_APPOINTED;
    }

    /**
     * 已物流提货
     */
    public function getDeliveredAttribute()
    {
        return ($this->master_status == Params::ORDER_MASTER_STATUS_LOGISTICS_OK)
            || ($this->master_status == Params::ORDER_MASTER_STATUS_LOGISTICS_FAILED);
    }

    /**
     * 已完成服务
     */
    public function getFinishedAttribute()
    {
        return $this->master_status == Params::ORDER_MASTER_STATUS_FINISHED;
    }

    /**
     * 已收款到账
     * success
     */
    public function getSuccessAttribute()
    {
        return $this->master_status == Params::ORDER_MASTER_STATUS_SUCCESS;
    }

    /**
     * 订单退款状态文字
     * refund_status_text
     */
    public function getRefundStatusTextAttribute()
    {
        return Params::$ORDER_REFUND_STATUS[$this->refund_status];
    }

    /**
     * 订单仲裁状态文字
     */
    public function getArbitrateStatusTextAttribute()
    {
        return Params::$ORDER_ARBITRATE_STATUS[$this->arbitrate_status];
    }

    /**
     * 订单异常状态文字
     */
    public function getExceptStatusTextAttribute()
    {
        return Params::$ORDER_EXCEPT_STATUS[$this->except_status];
    }

    /**
     * 订单商家端状态文字
     */
    public function getMerchantStatusTextAttribute()
    {
        return Params::$ORDER_MERCHANT_STATUS[$this->merchant_status];
    }

    /**
     * 预约上门时间文字
     */
    public function getDoorTimeTextAttribute()
    {
        return $this->door_time ? sprintf('%s(%s) %s',
            $this->door_time->format('m月d日'),
            Params::$WEEKS[$this->door_time->format('N')],
            Params::$AMPM[$this->door_time->format('a')]) : null;
    }

    /**
     * 预约上门详细时间段文字
     * door_time_period_text
     */
    public function getDoorTimePeriodTextAttribute()
    {
        return $this->door_time ? sprintf('%s(%s) %s',
            $this->door_time->format('m月d日'),
            Params::$WEEKS[$this->door_time->format('N')],
            Params::$HOURS_PERIOD[$this->door_time->format('G')]) : null;
    }

    /**
     * 物流提货时间文字
     */
    public function getDeliverTimeTextAttribute()
    {
        return $this->deliver_time ? sprintf('%s(%s) %s',
            $this->deliver_time->format('m月d日'),
            Params::$WEEKS[$this->deliver_time->format('N')],
            Params::$AMPM[$this->deliver_time->format('a')]) : null;
    }

    /**
     * 完成服务时间文字
     */
    public function getFinishTimeTextAttribute()
    {
        return $this->finish_time ? sprintf('%s(%s) %s',
            $this->finish_time->format('m月d日'),
            Params::$WEEKS[$this->finish_time->format('N')],
            Params::$AMPM[$this->finish_time->format('a')]) : null;
    }

    /**
     * 收款到账时间文字
     */
    public function getSuccessTimeTextAttribute()
    {
        return $this->order_success_time ? sprintf('%s(%s) %s',
            $this->order_success_time->format('m月d日'),
            Params::$WEEKS[$this->order_success_time->format('N')],
            Params::$AMPM[$this->order_success_time->format('a')]) : null;
    }

    /**
     * 物流提货照片url
     * deliver_imgs_url
     */
    public function getDeliverImgsUrlAttribute()
    {
        return Collection::make($this->deliver_imgs)->transform(function($item,$key){
            return resolve(IStorageService::class)->getUrl($item);
        });
    }

    /**
     * 完工照片url
     * finish_imgs_url
     */
    public function getFinishImgsUrlAttribute()
    {
        return Collection::make($this->finish_imgs)->transform(function($item,$key){
            return resolve(IStorageService::class)->getUrl($item);
        });
    }

    /**
     * 客户签收单照片url
     * finish_ticket_img_url
     */
    public function getFinishTicketImgUrlAttribute()
    {
        return resolve(IStorageService::class)->getUrl($this->finish_ticket_img);
    }

    /**
     * 喵师傅成功核销截图url
     * tmall_check_img_url
     */
    public function getTmallCheckImgUrlAttribute()
    {
        return resolve(IStorageService::class)->getUrl($this->tmall_check_img);
    }

    /**
     * 对师傅评价状态文字
     * evaluate_status_text
     */
    public function getEvaluateStatusTextAttribute()
    {
        return Params::$ORDER_EVALUATE_STATUS[$this->evaluate_status];
    }
}
